home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / direct3d / dolphinvs / dolphinvs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  26.2 KB  |  682 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Dolphin.cpp
  3. //
  4. // Desc: Sample of swimming dolphin
  5. //
  6. //       Note: This code uses the D3D Framework helper library.
  7. //
  8. // Copyright (c) 1998-2000 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <stdio.h>
  12. #include <D3DX8.h>
  13. #include "D3DApp.h"
  14. #include "D3DFile.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18.  
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Globals variables and definitions
  24. //-----------------------------------------------------------------------------
  25. #define WATER_COLOR         0x00004080
  26.  
  27. struct D3DVERTEX
  28. {
  29.     D3DXVECTOR3 p;
  30.     D3DXVECTOR3 n;
  31.     FLOAT       tu, tv;
  32. };
  33.  
  34. #define D3DFVF_D3DVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
  35.  
  36.  
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: class CMyD3DApplication
  41. // Desc: Main class to run this application. Most functionality is inherited
  42. //       from the CD3DApplication base class.
  43. //-----------------------------------------------------------------------------
  44. class CMyD3DApplication : public CD3DApplication
  45. {
  46.     // Font for drawing text
  47.     CD3DFont* m_pFont;
  48.  
  49.     // Transform matrices
  50.     D3DXMATRIX              m_matWorld;
  51.     D3DXMATRIX              m_matView;
  52.     D3DXMATRIX              m_matProj;
  53.  
  54.     // Dolphin object
  55.     LPDIRECT3DTEXTURE8      m_pDolphinTexture;
  56.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB1;
  57.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB2;
  58.     LPDIRECT3DVERTEXBUFFER8 m_pDolphinVB3;
  59.     LPDIRECT3DINDEXBUFFER8  m_pDolphinIB;
  60.     DWORD                   m_dwNumDolphinVertices;
  61.     DWORD                   m_dwNumDolphinFaces;
  62.     DWORD                   m_dwDolphinVertexShader;
  63.     DWORD                   m_dwDolphinVertexShader2;
  64.  
  65.     // Seafloor object
  66.     LPDIRECT3DTEXTURE8      m_pSeaFloorTexture;
  67.     LPDIRECT3DVERTEXBUFFER8 m_pSeaFloorVB;
  68.     LPDIRECT3DINDEXBUFFER8  m_pSeaFloorIB;
  69.     DWORD                   m_dwNumSeaFloorVertices;
  70.     DWORD                   m_dwNumSeaFloorFaces;
  71.     DWORD                   m_dwSeaFloorVertexShader;
  72.     DWORD                   m_dwSeaFloorVertexShader2;
  73.  
  74.     // Water caustics
  75.     LPDIRECT3DTEXTURE8      m_pCausticTextures[32];
  76.     LPDIRECT3DTEXTURE8      m_pCurrentCausticTexture;
  77.  
  78. public:
  79.     HRESULT OneTimeSceneInit();
  80.     HRESULT InitDeviceObjects();
  81.     HRESULT RestoreDeviceObjects();
  82.     HRESULT InvalidateDeviceObjects();
  83.     HRESULT DeleteDeviceObjects();
  84.     HRESULT Render();
  85.     HRESULT FrameMove();
  86.     HRESULT FinalCleanup();
  87.     HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
  88.  
  89.     CMyD3DApplication();
  90. };
  91.  
  92.  
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Name: WinMain()
  97. // Desc: Entry point to the program. Initializes everything, and goes into a
  98. //       message-processing loop. Idle time is used to render the scene.
  99. //-----------------------------------------------------------------------------
  100. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  101. {
  102.     CMyD3DApplication d3dApp;
  103.  
  104.     if( FAILED( d3dApp.Create( hInst ) ) )
  105.         return 0;
  106.  
  107.     return d3dApp.Run();
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Name: CMyD3DApplication()
  116. // Desc: Constructor
  117. //-----------------------------------------------------------------------------
  118. CMyD3DApplication::CMyD3DApplication()
  119. {
  120.     // Override base class members
  121.     m_strWindowTitle         = _T("DolphinVS: Tweening Vertex Shader");
  122.     m_bUseDepthBuffer        = TRUE;
  123.  
  124.     m_pFont                  = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  125.  
  126.     // Dolphin object
  127.     m_pDolphinTexture        = NULL;
  128.     m_pDolphinVB1            = NULL;
  129.     m_pDolphinVB2            = NULL;
  130.     m_pDolphinVB3            = NULL;
  131.     m_pDolphinIB             = NULL;
  132.     m_dwDolphinVertexShader  = 0L;
  133.     m_dwDolphinVertexShader2 = 0L;
  134.  
  135.     // SeaFloor object
  136.     m_pSeaFloorTexture       = NULL;
  137.     m_pSeaFloorVB            = NULL;
  138.     m_pSeaFloorIB            = NULL;
  139.     m_dwSeaFloorVertexShader = 0L;
  140.     m_dwSeaFloorVertexShader2= 0L;
  141.  
  142.     // Water caustics
  143.     for( DWORD t=0; t<32; t++ )
  144.         m_pCausticTextures[t] = NULL;
  145. }
  146.  
  147.  
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // Name: OneTimeSceneInit()
  152. // Desc: Called during initial app startup, this function performs all the
  153. //       permanent initialization.
  154. //-----------------------------------------------------------------------------
  155. HRESULT CMyD3DApplication::OneTimeSceneInit()
  156. {
  157.     return S_OK;
  158. }
  159.  
  160.  
  161.  
  162.  
  163. //-----------------------------------------------------------------------------
  164. // Name: FrameMove()
  165. // Desc: Called once per frame, the call is the entry point for animating
  166. //       the scene.
  167. //-----------------------------------------------------------------------------
  168. HRESULT CMyD3DApplication::FrameMove()
  169. {
  170.     // Animation attributes for the dolphin
  171.     FLOAT fKickFreq    = 2*m_fTime;
  172.     FLOAT fPhase       = m_fTime/3;
  173.     FLOAT fBlendWeight = sinf( fKickFreq );
  174.  
  175.     // Move the dolphin in a circle
  176.     D3DXMATRIX matDolphin, matTrans, matRotate1, matRotate2;
  177.     D3DXMatrixScaling( &matDolphin, 0.01f, 0.01f, 0.01f );
  178.     D3DXMatrixRotationZ( &matRotate1, -cosf(fKickFreq)/6 );
  179.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matRotate1 );
  180.     D3DXMatrixRotationY( &matRotate2, fPhase );
  181.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matRotate2 );
  182.     D3DXMatrixTranslation( &matTrans, -5*sinf(fPhase), sinf(fKickFreq)/2, 10-10*cosf(fPhase) );
  183.     D3DXMatrixMultiply( &matDolphin, &matDolphin, &matTrans );
  184.  
  185.     // Animate the caustic textures
  186.     DWORD tex = ((DWORD)(m_fTime*32))%32;
  187.     m_pCurrentCausticTexture = m_pCausticTextures[tex];
  188.  
  189.     // Set the vertex shader constants. Note: outside of the blend matrices,
  190.     // most of these values don't change, so don't need to really be set every
  191.     // frame. It's just done here for clarity
  192.     {
  193.         // Some basic constants
  194.         D3DXVECTOR4 vZero( 0.0f, 0.0f, 0.0f, 0.0f );
  195.         D3DXVECTOR4 vOne( 1.0f, 0.5f, 0.2f, 0.05f );
  196.  
  197.         FLOAT fWeight1;
  198.         FLOAT fWeight2;
  199.         FLOAT fWeight3;
  200.  
  201.         if( fBlendWeight > 0.0f )
  202.         {
  203.             fWeight1 = fabsf(fBlendWeight);
  204.             fWeight2 = 1.0f - fabsf(fBlendWeight);
  205.             fWeight3 = 0.0f;
  206.         }
  207.         else
  208.         {
  209.             fWeight1 = 0.0f;
  210.             fWeight2 = 1.0f - fabsf(fBlendWeight);
  211.             fWeight3 = fabsf(fBlendWeight);
  212.         }
  213.         D3DXVECTOR4 vWeight( fWeight1, fWeight2, fWeight3, 0.0f );
  214.  
  215.         // Lighting vector (normalized) and material colors. (Use red light
  216.         // to show difference from non-vertex shader case.)
  217.         FLOAT fLight[]    = { 0.0f, 1.0f, 0.0f, 0.0f };
  218.         FLOAT fDiffuse[]  = { 1.00f, 1.00f, 1.00f, 1.00f };
  219.         FLOAT fAmbient[]  = { 0.25f, 0.25f, 0.25f, 0.25f };
  220.         FLOAT fFog[]      = { 0.5f, 50.0f, 1.0f/(50.0f-1.0f), 0.0f };
  221.         FLOAT fCaustics[] = { 0.05f, 0.05f, sinf(m_fTime)/8, cosf(m_fTime)/10 };
  222.  
  223.         // Vertex shader operations use transposed matrices
  224.         D3DXMATRIX mat, matCamera, matTranspose, matCameraTranspose;
  225.         D3DXMATRIX matViewTranspose, matProjTranspose;
  226.         D3DXMatrixMultiply(&matCamera, &matDolphin, &m_matView);
  227.         D3DXMatrixMultiply(&mat, &matCamera, &m_matProj);
  228.         D3DXMatrixTranspose(&matTranspose, &mat);
  229.         D3DXMatrixTranspose(&matCameraTranspose, &matCamera);
  230.         D3DXMatrixTranspose(&matViewTranspose, &m_matView);
  231.         D3DXMatrixTranspose(&matProjTranspose, &m_matProj);
  232.  
  233.         // Set the vertex shader constants
  234.         m_pd3dDevice->SetVertexShaderConstant(  0, &vZero,     1 );
  235.         m_pd3dDevice->SetVertexShaderConstant(  1, &vOne,      1 );
  236.         m_pd3dDevice->SetVertexShaderConstant(  2, &vWeight,   1 );
  237.         m_pd3dDevice->SetVertexShaderConstant(  4, &matTranspose, 4 );
  238.         m_pd3dDevice->SetVertexShaderConstant(  8, &matCameraTranspose,  4 );
  239.         m_pd3dDevice->SetVertexShaderConstant( 12, &matViewTranspose,  4 );
  240.         m_pd3dDevice->SetVertexShaderConstant( 20, &fLight,    1 );
  241.         m_pd3dDevice->SetVertexShaderConstant( 21, &fDiffuse,  1 );
  242.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbient,  1 );
  243.         m_pd3dDevice->SetVertexShaderConstant( 23, &fFog,      1 );
  244.         m_pd3dDevice->SetVertexShaderConstant( 24, &fCaustics, 1 );
  245.         m_pd3dDevice->SetVertexShaderConstant( 28, &matProjTranspose,  4 );
  246.     }
  247.  
  248.     return S_OK;
  249. }
  250.  
  251.  
  252.  
  253.  
  254. //-----------------------------------------------------------------------------
  255. // Name: Render()
  256. // Desc: Called once per frame, the call is the entry point for 3d
  257. //       rendering. This function sets up render states, clears the
  258. //       viewport, and renders the scene.
  259. //-----------------------------------------------------------------------------
  260. HRESULT CMyD3DApplication::Render()
  261. {
  262.     // Clear the viewport
  263.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  264.                          WATER_COLOR, 1.0f, 0L );
  265.  
  266.     // Begin the scene
  267.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  268.     {
  269.         FLOAT fAmbientLight[]  = { 0.25f, 0.25f, 0.25f, 0.25f };
  270.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbientLight, 1 );
  271.  
  272.         // Render the seafloor
  273.         m_pd3dDevice->SetTexture( 0, m_pSeaFloorTexture );
  274.         m_pd3dDevice->SetVertexShader( m_dwSeaFloorVertexShader );
  275.         m_pd3dDevice->SetStreamSource( 0, m_pSeaFloorVB, sizeof(D3DVERTEX) );
  276.         m_pd3dDevice->SetIndices( m_pSeaFloorIB, 0 );
  277.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  278.                                             0, m_dwNumSeaFloorVertices,
  279.                                             0, m_dwNumSeaFloorFaces );
  280.  
  281.         // Render the dolphin
  282.         m_pd3dDevice->SetTexture( 0, m_pDolphinTexture );
  283.         m_pd3dDevice->SetVertexShader( m_dwDolphinVertexShader );
  284.         m_pd3dDevice->SetStreamSource( 0, m_pDolphinVB1, sizeof(D3DVERTEX) );
  285.         m_pd3dDevice->SetStreamSource( 1, m_pDolphinVB2, sizeof(D3DVERTEX) );
  286.         m_pd3dDevice->SetStreamSource( 2, m_pDolphinVB3, sizeof(D3DVERTEX) );
  287.         m_pd3dDevice->SetIndices( m_pDolphinIB, 0 );
  288.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  289.                                             0, m_dwNumDolphinVertices,
  290.                                             0, m_dwNumDolphinFaces );
  291.  
  292.         // Now, we are going to do a 2nd pass, to alpha-blend in the caustics.
  293.         // The caustics use a 2nd set of texture coords that are generated
  294.         // by the vertex shaders. Lighting from the light above is used, by
  295.         // ambient is turned off to avoid lighting objects from below (for
  296.         // instance, we don't want caustics appearing on the dolphin's
  297.         // underbelly). Finally, fog color is set to black, so that caustics
  298.         // fade in distance.
  299.  
  300.         // Turn on alpha blending
  301.         m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  302.         m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCCOLOR );
  303.         m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
  304.  
  305.         // Setup the caustic texture
  306.         m_pd3dDevice->SetTexture( 0, m_pCurrentCausticTexture );
  307.  
  308.         // Set ambient and fog colors to black
  309.         FLOAT fAmbientDark[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  310.         m_pd3dDevice->SetVertexShaderConstant( 22, &fAmbientDark, 1 );
  311.         m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR, 0x00000000 );
  312.  
  313.         // Render the caustic effects for the seafloor
  314.         m_pd3dDevice->SetVertexShader( m_dwSeaFloorVertexShader2 );
  315.         m_pd3dDevice->SetStreamSource( 0, m_pSeaFloorVB, sizeof(D3DVERTEX) );
  316.         m_pd3dDevice->SetIndices( m_pSeaFloorIB, 0 );
  317.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  318.                                             0, m_dwNumSeaFloorVertices,
  319.                                             0, m_dwNumSeaFloorFaces );
  320.  
  321.         // Finally, render the caustic effects for the dolphin
  322.         m_pd3dDevice->SetVertexShader( m_dwDolphinVertexShader2 );
  323.         m_pd3dDevice->SetStreamSource( 0, m_pDolphinVB1, sizeof(D3DVERTEX) );
  324.         m_pd3dDevice->SetStreamSource( 1, m_pDolphinVB2, sizeof(D3DVERTEX) );
  325.         m_pd3dDevice->SetStreamSource( 2, m_pDolphinVB3, sizeof(D3DVERTEX) );
  326.         m_pd3dDevice->SetIndices( m_pDolphinIB, 0 );
  327.         m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  328.                                             0, m_dwNumDolphinVertices,
  329.                                             0, m_dwNumDolphinFaces );
  330.  
  331.         // Restore modified render states
  332.         m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  333.         m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR, WATER_COLOR );
  334.  
  335.         // Output statistics
  336.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  337.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  338.  
  339.         // End the scene.
  340.         m_pd3dDevice->EndScene();
  341.     }
  342.  
  343.     return S_OK;
  344. }
  345.  
  346.  
  347.  
  348.  
  349. //-----------------------------------------------------------------------------
  350. // Name: InitDeviceObjects()
  351. // Desc: Initialize device-dependent objects. This is the place to create mesh
  352. //       and texture objects.
  353. //-----------------------------------------------------------------------------
  354. HRESULT CMyD3DApplication::InitDeviceObjects()
  355. {
  356.     LPDIRECT3DVERTEXBUFFER8 pMeshSourceVB;
  357.     LPDIRECT3DINDEXBUFFER8  pMeshSourceIB;
  358.     D3DVERTEX*              pSrc;
  359.     D3DVERTEX*              pDst;
  360.     CD3DMesh                DolphinMesh01;
  361.     CD3DMesh                DolphinMesh02;
  362.     CD3DMesh                DolphinMesh03;
  363.     CD3DMesh                SeaFloorMesh;
  364.  
  365.     // Initialize the font's internal textures
  366.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  367.  
  368.     // Create texture for the dolphin
  369.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Dolphin.bmp"),
  370.                                        &m_pDolphinTexture ) ) )
  371.     {
  372.         return D3DAPPERR_MEDIANOTFOUND;
  373.     }
  374.  
  375.     // Create textures for the seafloor
  376.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("SeaFloor.bmp"),
  377.                                        &m_pSeaFloorTexture ) ) )
  378.     {
  379.         return D3DAPPERR_MEDIANOTFOUND;
  380.     }
  381.  
  382.     // Create textures for the water caustics
  383.     for( DWORD t=0; t<32; t++ )
  384.     {
  385.         TCHAR strName[80];
  386.         sprintf( strName, _T("Caust%02ld.tga"), t );
  387.         if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, strName,
  388.                                            &m_pCausticTextures[t] ) ) )
  389.         {
  390.             return D3DAPPERR_MEDIANOTFOUND;
  391.         }
  392.     }
  393.  
  394.     // Load the file-based mesh objects
  395.     if( FAILED( DolphinMesh01.Create( m_pd3dDevice, _T("dolphin1.x") ) ) )
  396.         return D3DAPPERR_MEDIANOTFOUND;
  397.     if( FAILED( DolphinMesh02.Create( m_pd3dDevice, _T("dolphin2.x") ) ) )
  398.         return D3DAPPERR_MEDIANOTFOUND;
  399.     if( FAILED( DolphinMesh03.Create( m_pd3dDevice, _T("dolphin3.x") ) ) )
  400.         return D3DAPPERR_MEDIANOTFOUND;
  401.     if( FAILED( SeaFloorMesh.Create( m_pd3dDevice, _T("SeaFloor.x") ) ) )
  402.         return D3DAPPERR_MEDIANOTFOUND;
  403.  
  404.     // Set the FVF type to match the vertex format we want
  405.     DolphinMesh01.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  406.     DolphinMesh02.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  407.     DolphinMesh03.SetFVF( m_pd3dDevice, D3DFVF_D3DVERTEX );
  408.     SeaFloorMesh.SetFVF(  m_pd3dDevice, D3DFVF_D3DVERTEX );
  409.  
  410.     // Get the number of vertices and faces for the meshes
  411.     m_dwNumDolphinVertices  = DolphinMesh01.GetSysMemMesh()->GetNumVertices();
  412.     m_dwNumDolphinFaces     = DolphinMesh01.GetSysMemMesh()->GetNumFaces();
  413.     m_dwNumSeaFloorVertices = SeaFloorMesh.GetSysMemMesh()->GetNumVertices();
  414.     m_dwNumSeaFloorFaces    = SeaFloorMesh.GetSysMemMesh()->GetNumFaces();
  415.  
  416.     // Create the dolphin and seafloor vertex and index buffers
  417.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  418.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  419.                                       &m_pDolphinVB1 );
  420.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  421.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  422.                                       &m_pDolphinVB2 );
  423.     m_pd3dDevice->CreateVertexBuffer( m_dwNumDolphinVertices * sizeof(D3DVERTEX),
  424.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  425.                                       &m_pDolphinVB3 );
  426.     m_pd3dDevice->CreateVertexBuffer( m_dwNumSeaFloorVertices * sizeof(D3DVERTEX),
  427.                                       D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
  428.                                       &m_pSeaFloorVB );
  429.     m_pd3dDevice->CreateIndexBuffer( m_dwNumDolphinFaces * 3 * sizeof(WORD),
  430.                                       D3DUSAGE_WRITEONLY,
  431.                                       D3DFMT_INDEX16, D3DPOOL_MANAGED,
  432.                                       &m_pDolphinIB );
  433.     m_pd3dDevice->CreateIndexBuffer( m_dwNumSeaFloorFaces * 3 * sizeof(WORD),
  434.                                       D3DUSAGE_WRITEONLY,
  435.                                       D3DFMT_INDEX16, D3DPOOL_MANAGED,
  436.                                       &m_pSeaFloorIB );
  437.  
  438.     // Copy vertices for mesh 01
  439.     DolphinMesh01.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  440.     m_pDolphinVB1->Lock( 0, 0, (BYTE**)&pDst, 0 );
  441.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  442.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  443.     m_pDolphinVB1->Unlock();
  444.     pMeshSourceVB->Unlock();
  445.     pMeshSourceVB->Release();
  446.  
  447.     // Copy vertices for mesh 2
  448.     DolphinMesh02.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  449.     m_pDolphinVB2->Lock( 0, 0, (BYTE**)&pDst, 0 );
  450.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  451.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  452.     m_pDolphinVB2->Unlock();
  453.     pMeshSourceVB->Unlock();
  454.     pMeshSourceVB->Release();
  455.  
  456.     // Copy vertices for mesh 3
  457.     DolphinMesh03.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  458.     m_pDolphinVB3->Lock( 0, 0, (BYTE**)&pDst, 0 );
  459.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  460.     memcpy( pDst, pSrc, m_dwNumDolphinVertices * sizeof(D3DVERTEX) );
  461.     m_pDolphinVB3->Unlock();
  462.     pMeshSourceVB->Unlock();
  463.     pMeshSourceVB->Release();
  464.  
  465.     // Copy vertices for the seafloor mesh, and add some bumpiness
  466.     SeaFloorMesh.GetSysMemMesh()->GetVertexBuffer( &pMeshSourceVB );
  467.     m_pSeaFloorVB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  468.     pMeshSourceVB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  469.     memcpy( pDst, pSrc, m_dwNumSeaFloorVertices * sizeof(D3DVERTEX) );
  470.     srand(5);
  471.     for( DWORD i=0; i<m_dwNumSeaFloorVertices; i++ )
  472.     {
  473.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  474.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  475.         ((D3DVERTEX*)pDst)[i].p.y += (rand()/(FLOAT)RAND_MAX);
  476.         ((D3DVERTEX*)pDst)[i].tu  *= 10;
  477.         ((D3DVERTEX*)pDst)[i].tv  *= 10;
  478.     }
  479.     m_pSeaFloorVB->Unlock();
  480.     pMeshSourceVB->Unlock();
  481.     pMeshSourceVB->Release();
  482.  
  483.     // Copy indices for the dolphin mesh
  484.     DolphinMesh01.GetSysMemMesh()->GetIndexBuffer( &pMeshSourceIB );
  485.     m_pDolphinIB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  486.     pMeshSourceIB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  487.     memcpy( pDst, pSrc, 3 * m_dwNumDolphinFaces * sizeof(WORD) );
  488.     m_pDolphinIB->Unlock();
  489.     pMeshSourceIB->Unlock();
  490.     pMeshSourceIB->Release();
  491.  
  492.     // Copy indices for the seafloor mesh
  493.     SeaFloorMesh.GetSysMemMesh()->GetIndexBuffer( &pMeshSourceIB );
  494.     m_pSeaFloorIB->Lock( 0, 0, (BYTE**)&pDst, 0 );
  495.     pMeshSourceIB->Lock( 0, 0, (BYTE**)&pSrc, 0 );
  496.     memcpy( pDst, pSrc, 3 * m_dwNumSeaFloorFaces * sizeof(WORD) );
  497.     m_pSeaFloorIB->Unlock();
  498.     pMeshSourceIB->Unlock();
  499.     pMeshSourceIB->Release();
  500.  
  501.     return S_OK;
  502. }
  503.  
  504.  
  505.  
  506.  
  507. //-----------------------------------------------------------------------------
  508. // Name: RestoreDeviceObjects()
  509. // Desc: Restore device-memory objects and state after a device is created or
  510. //       resized.
  511. //-----------------------------------------------------------------------------
  512. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  513. {
  514.     HRESULT hr;
  515.  
  516.     m_pFont->RestoreDeviceObjects();
  517.  
  518.     // Set the transform matrices
  519.     D3DXVECTOR3 vEyePt      = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
  520.     D3DXVECTOR3 vLookatPt   = D3DXVECTOR3( 0.0f, 0.0f,  0.0f );
  521.     D3DXVECTOR3 vUpVec      = D3DXVECTOR3( 0.0f, 1.0f,  0.0f );
  522.     FLOAT       fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  523.     D3DXMatrixIdentity( &m_matWorld );
  524.     D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
  525.     D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/3, fAspect, 1.0f, 10000.0f );
  526.  
  527.     // Set default render states
  528.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  529.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  530.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
  531.     m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE,      TRUE );
  532.     m_pd3dDevice->SetRenderState( D3DRS_FOGCOLOR,       WATER_COLOR );
  533.  
  534.     // Create vertex shader for the dolphin
  535.     DWORD dwDolphinVertexDecl[] =
  536.     {
  537.         D3DVSD_STREAM( 0 ),
  538.         D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh
  539.         D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal
  540.         D3DVSD_REG( 6, D3DVSDT_FLOAT2 ), // Tex coords
  541.         D3DVSD_STREAM( 1 ),
  542.         D3DVSD_REG( 1, D3DVSDT_FLOAT3 ), // Position of second mesh
  543.         D3DVSD_REG( 4, D3DVSDT_FLOAT3 ), // Normal
  544.         D3DVSD_REG( 7, D3DVSDT_FLOAT2 ), // Tex coords
  545.         D3DVSD_STREAM( 2 ),
  546.         D3DVSD_REG( 2, D3DVSDT_FLOAT3 ), // Position of second mesh
  547.         D3DVSD_REG( 5, D3DVSDT_FLOAT3 ), // Normal
  548.         D3DVSD_REG( 8, D3DVSDT_FLOAT2 ), // Tex coords
  549.         D3DVSD_END()
  550.     };
  551.  
  552.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("DolphinTween.vsh"),
  553.                                                  dwDolphinVertexDecl,
  554.                                                  &m_dwDolphinVertexShader ) ) )
  555.     {
  556.         return hr;
  557.     }
  558.  
  559.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("DolphinTween2.vsh"),
  560.                                                  dwDolphinVertexDecl,
  561.                                                  &m_dwDolphinVertexShader2 ) ) )
  562.     {
  563.         return hr;
  564.     }
  565.  
  566.     // Create vertex shader for the seafloor
  567.     DWORD dwSeaFloorVertexDecl[] =
  568.     {
  569.         D3DVSD_STREAM( 0 ),
  570.         D3DVSD_REG( 0, D3DVSDT_FLOAT3 ), // Position of first mesh
  571.         D3DVSD_REG( 3, D3DVSDT_FLOAT3 ), // Normal
  572.         D3DVSD_REG( 6, D3DVSDT_FLOAT2 ), // Tex coords
  573.         D3DVSD_END()
  574.     };
  575.  
  576.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("SeaFloor.vsh"),
  577.                                                  dwSeaFloorVertexDecl,
  578.                                                  &m_dwSeaFloorVertexShader ) ) )
  579.     {
  580.         return hr;
  581.     }
  582.  
  583.     if( FAILED( hr = D3DUtil_CreateVertexShader( m_pd3dDevice, _T("SeaFloor2.vsh"),
  584.                                                  dwSeaFloorVertexDecl,
  585.                                                  &m_dwSeaFloorVertexShader2 ) ) )
  586.     {
  587.         return hr;
  588.     }
  589.  
  590.     return S_OK;
  591. }
  592.  
  593.  
  594.  
  595.  
  596. //-----------------------------------------------------------------------------
  597. // Name: InvalidateDeviceObjects()
  598. // Desc: Called when the device-dependent objects are about to be lost.
  599. //-----------------------------------------------------------------------------
  600. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  601. {
  602.     m_pFont->InvalidateDeviceObjects();
  603.  
  604.     // Clean up vertex shaders
  605.     m_pd3dDevice->DeleteVertexShader( m_dwDolphinVertexShader );
  606.     m_pd3dDevice->DeleteVertexShader( m_dwDolphinVertexShader2 );
  607.     m_pd3dDevice->DeleteVertexShader( m_dwSeaFloorVertexShader );
  608.     m_pd3dDevice->DeleteVertexShader( m_dwSeaFloorVertexShader2 );
  609.  
  610.     return S_OK;
  611. }
  612.  
  613.  
  614.  
  615.  
  616. //-----------------------------------------------------------------------------
  617. // Name: DeleteDeviceObjects()
  618. // Desc: Called when the app is exiting, or the device is being changed,
  619. //       this function deletes any device dependent objects.
  620. //-----------------------------------------------------------------------------
  621. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  622. {
  623.     m_pFont->DeleteDeviceObjects();
  624.  
  625.     // Clean up dolphin objects
  626.     SAFE_RELEASE( m_pDolphinTexture );
  627.     SAFE_RELEASE( m_pDolphinVB1 );
  628.     SAFE_RELEASE( m_pDolphinVB2 );
  629.     SAFE_RELEASE( m_pDolphinVB3 );
  630.     SAFE_RELEASE( m_pDolphinIB );
  631.  
  632.     // Clean up seafoor objects
  633.     SAFE_RELEASE( m_pSeaFloorTexture );
  634.     SAFE_RELEASE( m_pSeaFloorVB );
  635.     SAFE_RELEASE( m_pSeaFloorIB );
  636.  
  637.     // Clean up textures for water caustics
  638.     for( DWORD i=0; i<32; i++ )
  639.         SAFE_RELEASE( m_pCausticTextures[i] );
  640.  
  641.     return S_OK;
  642. }
  643.  
  644.  
  645.  
  646.  
  647. //-----------------------------------------------------------------------------
  648. // Name: FinalCleanup()
  649. // Desc: Called before the app exits, this function gives the app the chance
  650. //       to cleanup after itself.
  651. //-----------------------------------------------------------------------------
  652. HRESULT CMyD3DApplication::FinalCleanup()
  653. {
  654.     SAFE_DELETE( m_pFont );
  655.     return S_OK;
  656. }
  657.  
  658.  
  659.  
  660.  
  661. //-----------------------------------------------------------------------------
  662. // Name: ConfirmDevice()
  663. // Desc: Called during device initialization, this code checks the device
  664. //       for some minimum set of capabilities
  665. //-----------------------------------------------------------------------------
  666. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  667.                                           D3DFORMAT Format )
  668. {
  669.     if( ( dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||
  670.         ( dwBehavior & D3DCREATE_MIXED_VERTEXPROCESSING ) )
  671.     {
  672.         if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,0) )
  673.             return E_FAIL;
  674.     }
  675.  
  676.     return S_OK;
  677. }
  678.  
  679.  
  680.  
  681.  
  682.